<?php namespace App\Http\Controllers;

use Auth;
use Hash;
use App\User;
use Redirect;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class SignController extends Controller {

    /**
     * 返回login视图,登录页面
     */
    public function sign_up()
    {
        return view('sign.up');
    }
    /**
     * 返回login视图,登录页面
     */
    public function sign_in()
    {
        return view('sign.in');
    }


    /**
     * 登录响应
     */
    public function in_post(Request $request)
    {
        $this->validate($request, User::rules());
        $id = $request->get('id');
        $password = $request->get('password');
        if (Auth::attempt(['username' => $id, 'password' => $password], $request->get('remember'))) {
            if (!Auth::user()->is_admin) {
                return Redirect::route('index');
            } else {
                return Redirect::action('Admin\AdminController@index');
            }

        }
        elseif (Auth::attempt(['phone' => $id, 'password' => $password], $request->get('remember'))) {
            if (!Auth::user()->is_admin) {
                return Redirect::route('index');
            } else {
                return Redirect::action('Admin\AdminController@index');
            }

        } else {
            return Redirect::route('login')
                ->withInput()
                ->withErrors('学号或者密码不正确，请重试！');
        }
    }
    /**
     * 注册响应
     */
    public function up_post(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required|digits:10|unique:users',
//        ]);
        $user = new User;
        $user->phone = $request->phone;
        $user->sex = $request->sex;
        $user->company = $request->company;
        $user->username = $request->username;
        $user->name = $request->name;
        $user->password = Hash::make($request->password);
        $user->save();
        session()->flash('message', $user->name."注册成功");
//        DB::insert('insert into grades (user_id, math, english, c, sport, think,soft)
//            values (?,?,?,?,?,?,?)', [$request->id,null,null,null,null,null,null]);
        return Redirect::to('admin');
    }

	public function licence()
    {
        return view('sign.licence');
    }


}
